using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;

public class PatrolEditor : EditorWindow {

    private static bool _init;

    private GameObject _prefabEnemy;
    private int _nbLevelPassages;
    private int _currentPassage;
    private List<Enemy> _enemys;
    private Enemy _currentEnemy;
    private int _currentIndexEnemy;
    private Const.ENNEMY_TYPE _futureEnemyType;
    private LevelEditorManager _lvlEditor;
    private Tile _currentTileSelected;
    private STATE_TILE _stateTileSelection;
    private Vector2 _archeoStartPos;

    private Color _startColor;
    private Color _pathCurrentEnemyColor;
    private Color _pathOtherEnemyColor;


    private enum STATE_TILE { NONE, START_POS, PATH, SUPR, START_ARCHEO };

    [MenuItem("ExplorerZ/Patrol Creations")]
    static void Init()
    {
        _init = false;

        // Get existing open window or if none, make a new one:
        PatrolEditor window = (PatrolEditor)EditorWindow.GetWindow(typeof(PatrolEditor));
        window.Repaint();        
    }


    void OnDestroy()
    {
        _lvlEditor.stateEditor = LevelEditorManager.STATE_EDITOR.DECORS;
    }

    void Initialize()
    {
        if (_prefabEnemy == null)
        {
            _prefabEnemy = Resources.Load("Prefabs/Enemys/Enemy") as GameObject;
        }

        _lvlEditor = FindObjectOfType(typeof(LevelEditorManager)) as LevelEditorManager;
        _lvlEditor.stateEditor = LevelEditorManager.STATE_EDITOR.ENEMYS;
        _enemys = _lvlEditor.enemyToSave;
        _currentPassage = _lvlEditor.nbPassages;
        _archeoStartPos = _lvlEditor.archeoStartPos;

        if (_enemys == null)
        {
            _enemys = new List<Enemy>();
        }

        _stateTileSelection = STATE_TILE.NONE;
        //_currentPassage = 1;
        _currentTileSelected = null;
        _startColor = Color.green;
        _pathCurrentEnemyColor = Color.red;
        _pathOtherEnemyColor = Color.blue;

        _init = true;
    }

    void OnGUI()
    {
        if (!_init)
        {
            Initialize();
        }

        //Num Passage _nbLevelPassages
        _nbLevelPassages = EditorGUILayout.IntField("Nb Passages : ", _nbLevelPassages);

        if (GUILayout.Button("Start Pos Archeo"))
        {
            _stateTileSelection = STATE_TILE.START_ARCHEO;
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        PersoCreation();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        PersoSelection();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        PersoParams();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        EditorGUILayout.Space();
        EditorGUILayout.Space();

        if (GUILayout.Button("Save"))
        {
            _lvlEditor.enemyToSave = _enemys;
            _lvlEditor.nbPassages = _nbLevelPassages;
            _lvlEditor.archeoStartPos = _archeoStartPos;
            _lvlEditor.Save();
        }
    }


    void PersoCreation()
    {
        EditorGUILayout.BeginHorizontal();
        _futureEnemyType = (Const.ENNEMY_TYPE)EditorGUILayout.EnumPopup(_futureEnemyType);

        if (GUILayout.Button("Create Perso"))
        {
            GameObject newEnemy = GameObject.Instantiate(_prefabEnemy) as GameObject;
            newEnemy.name = "Enemy " + (_enemys.Count + 1).ToString();
            _currentEnemy = newEnemy.GetComponent<Enemy>();
            _enemys.Add(_currentEnemy);
            _currentEnemy.Type = _futureEnemyType;
            _lvlEditor.currentTileSelected = null;

        }

        EditorGUILayout.EndHorizontal();
    }

    void PersoSelection()
    {
        string[] nameEnemys = new string[_enemys.Count];
        int index = 0;

        foreach (Enemy enemy in _enemys)
        {
            if (enemy == _currentEnemy)
                _currentIndexEnemy = index;

            nameEnemys[index++] = enemy.name;
        }
        int saveIndex = _currentIndexEnemy;


        EditorGUILayout.BeginHorizontal();
        _currentIndexEnemy = EditorGUILayout.Popup(_currentIndexEnemy, nameEnemys);

        //Delete Perso
        if (GUILayout.Button("Delete"))
        {
            Destroy(_enemys[_currentIndexEnemy].gameObject);
            _enemys.RemoveAt(_currentIndexEnemy);
            ResetTilesColor();
            _currentIndexEnemy = 0;
        }
        EditorGUILayout.EndHorizontal();

        if (_enemys.Count > 0)
        {
            _currentEnemy = _enemys[_currentIndexEnemy];

            ResetTilesColor();
        }
    }

    void PersoParams()
    {
        if (_currentEnemy == null)
            return;

        _currentEnemy.MinNumPassage = EditorGUILayout.IntField(_currentEnemy.MinNumPassage);

        //Button Start Position
        if (GUILayout.Button("Set Start Pos"))
        {
            _stateTileSelection = STATE_TILE.START_POS;
        }

        //Button Add to Path
        if (GUILayout.Button("Add to Path"))
        {
            _stateTileSelection = STATE_TILE.PATH;
        }

        if (GUILayout.Button("Suppr to Path"))
        {
             _stateTileSelection = STATE_TILE.SUPR;
        }

        if (GUILayout.Button("Delete Path"))
        {
            EnemyMove enemyMove = _currentEnemy.GetComponent<EnemyMove>();

            foreach (Vector2 v in enemyMove.path)
            {
                _lvlEditor.GetTileAt(enemyMove.startPosition).renderer.material.color = Color.white;
            }
            enemyMove.path.Clear();

            _lvlEditor.GetTileAt(enemyMove.startPosition).renderer.material.color = Color.white;
            enemyMove.startPosition = new Vector2();
            ResetTilesColor();
            return;
        }

        //Tile Selected
        if (_lvlEditor.currentTileSelected != null)
        {
            _currentTileSelected = _lvlEditor.currentTileSelected;

            EnemyMove enemyMove = _currentEnemy.GetComponent<EnemyMove>();

            //Set Start Pos
            if (_stateTileSelection == STATE_TILE.START_POS)
            {
                _lvlEditor.GetTileAt(enemyMove.startPosition).renderer.material.color = Color.white;

                enemyMove.startPosition = _currentTileSelected.position2D;
                _lvlEditor.currentTileSelected.renderer.material.color = _startColor;
            }
            //Add to path
            else if (_stateTileSelection == STATE_TILE.PATH)
            {
                enemyMove.path.Add(_currentTileSelected.position2D);
                _lvlEditor.currentTileSelected.renderer.material.color = _pathCurrentEnemyColor;
            }
            //Remove to path
            else if (_stateTileSelection == STATE_TILE.SUPR)
            {
                int indexToRemove = 0;
                foreach (Vector2 v in _currentEnemy.GetComponent<EnemyMove>().path)
                {
                    if(_lvlEditor.GetTileAt(v) == _currentTileSelected)
                    {
                        enemyMove.path.RemoveAt(indexToRemove);
                        _lvlEditor.GetTileAt(v).renderer.material.color = Color.white;
                    }
                    ++indexToRemove;
                }
            }
            //Start Pos Level
            else if (_stateTileSelection == STATE_TILE.START_ARCHEO)
            {
                _archeoStartPos = _currentTileSelected.position2D;
            }

            _lvlEditor.currentTileSelected = null;
        }

        
    }

    void ResetTilesColor()
    {
        //Current Enemy Color
        EnemyMove enemyMove = _currentEnemy.GetComponent<EnemyMove>();
        _lvlEditor.tiles[(int)enemyMove.startPosition.x][(int)enemyMove.startPosition.y].renderer.material.color = _startColor;

        foreach (Vector2 v in enemyMove.path)
        {
            _lvlEditor.tiles[(int)v.x][(int)v.y].renderer.material.color = _pathCurrentEnemyColor;
        }

        //Other Enemys
        foreach (Enemy enemy in _enemys)
        {
            if(enemy != _currentEnemy)
            {
                EnemyMove otherEnemy = enemy.GetComponent<EnemyMove>();
                _lvlEditor.tiles[(int)otherEnemy.startPosition.x][(int)otherEnemy.startPosition.y].renderer.material.color = Color.white;

                foreach (Vector2 v in enemy.GetComponent<EnemyMove>().path)
                {
                    _lvlEditor.tiles[(int)v.x][(int)v.y].renderer.material.color = _pathOtherEnemyColor;
                }
            }
        }
    }
}
